package com.mmall.concurrency.example.aqs.phaser;

import java.util.Random;

/**
 * 模拟人以及旅游的各类状态
 */
public class Colleague implements Runnable {
    private TravelPhaser travelPhaser;
    public Colleague(TravelPhaser travelPhaser) {
        this.travelPhaser = travelPhaser;
    }

    /**
     * 模拟每位同事的动作
     */
    @Override
    public void run() {
        doAnything();
        System.out.println(Thread.currentThread().getName() + "到达出发集合地");
        travelPhaser.arriveAndAwaitAdvance();

        doAnything();
        System.out.println(Thread.currentThread().getName() + "已经在景点 A 自由活动结束");
        travelPhaser.arriveAndAwaitAdvance();

        doAnything();
        System.out.println(Thread.currentThread().getName() + "已经在景点 B 自由活动结束");
        travelPhaser.arriveAndAwaitAdvance();

        doAnything();
        System.out.println(Thread.currentThread().getName() + "到达返程集合地");
        travelPhaser.arriveAndAwaitAdvance();
    }

    /**
     * 模拟用时
     */
    private void doAnything() {
        try {
            Thread.sleep(new Random().nextInt(10000));
        } catch (Exception e) {}
    }
}
